// JavaScript Document
menuTree = function () {
	this.structureCashe = new Array();
	this.branchCashe = new Array();
	this.branchInitAdaptor = function (self) { };
}

menuTree.prototype.getObject = function(obj) {
	return typeof obj=='object' ? obj : document.getElementById(obj);
}

menuTree.prototype.reloadBranches = function(from_id,depth) {
	if (typeof this.branchCashe[from_id] == 'undefined') {
		return false;
	}
	
	while (depth>0) {
		var parent_id = this.branchCashe[from_id].parent.id;
		if (parent_id==0) break;
		var from_id = parent_id;
	}
	
	//debug('Rebuild: '+from_id);
	
	if (from_id!=0) {
		var cashedObj = this.branchCashe[from_id];
		var triggerId = cashedObj.triggerId;
		var submenuId = cashedObj.submenuId;
		var this_id = cashedObj.id;
		var root_id = cashedObj.parent.id;
		
		var collapsed = cashedObj.collapsed;
	}
	
	var self = this;
	window.setTimeout(function () {
			if (from_id!=0) { 
				var newBranch = self.initBranch(this_id,root_id,triggerId,submenuId);
				
				if (collapsed==1) {
					newBranch.show();
				}
			}
			//debug(self.structureCashe[from_id].length + 'Subbranches on '+from_id+' element');
			if (typeof self.structureCashe[from_id] == 'object') {
				for (var o=0; o<self.structureCashe[from_id].length; o++) {
					self.reloadBranches(self.structureCashe[from_id][o],0);
				}
			}
		}, 10);
}

menuTree.prototype.initBranch = function (this_id,root_id,triggerObj,submenuObj) {
	var self = this;
	var triggerObj = this.getObject(triggerObj);
	var submenuObj = this.getObject(submenuObj);
	var newBranch = new this.Branch(this.branchCashe[root_id],this_id,triggerObj,submenuObj,this.branchInitAdaptor);
	
	this.branchCashe[this_id] = newBranch;
	
	if (typeof this.structureCashe[this_id] == 'undefined') {
		this.structureCashe[this_id] = new Array();
	}
	if (typeof this.structureCashe[root_id] == 'undefined') {
		this.structureCashe[root_id] = new Array();
	}
	
	var push_f = true;
	for (var s=0;s<this.structureCashe[root_id].length; s++) {
		if (this.structureCashe[root_id][s]==this_id) {
			push_f = false;
		}
	}
	if (push_f==true) {
		this.structureCashe[root_id].push(this_id);
	}
	
	return newBranch;
	
}

menuTree.prototype.Branch = function (parent,this_id,triggerObj,submenuObj,adaptor) {
	this.parent = typeof parent == 'object' ? parent : false;
	this.collapsed = 0;
	this.id = this_id;
	this.triggerObj = triggerObj;
	this.submenuObj = submenuObj;
	this.triggerId = triggerObj.id;
	this.submenuId = submenuObj.id;
	this.popupAreaObject = new PopupArea(triggerObj,submenuObj,function () { });
	adaptor(this);
}

menuTree.prototype.Branch.prototype.show = function () {
	this.popupAreaObject.show(this.popupAreaObject);
	this.collapsed = 1;
}

menuTree.prototype.Branch.prototype.hide = function () {
	this.popupAreaObject.do_hide(this.popupAreaObject);
	this.collapsed = 0;
}
